package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.das.mechanic_base.R;

import java.util.ArrayList;
import java.util.List;

public class X3LoginCodeView extends RelativeLayout {

    private Context mContext;

    private InputMethodManager methodManager;

    private List<String> codeList = new ArrayList<>();
    private TextView tv_code1;
    private TextView tv_code2;
    private TextView tv_code3;
    private TextView tv_code4;
    private TextView tv_code5;
    private TextView tv_code6;
    private EditText et_code;
    private View v1;
    private View v2;
    private View v3;
    private View v4;
    private View v5;
    private View v6;

    public X3LoginCodeView(Context context) {
        this(context,null);
    }

    public X3LoginCodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
        initEvent();
    }

    private void initEvent() {
        //验证码输入
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
                if(editable != null && editable.length() > 0) {
                    et_code.setText("");
                    if(codeList.size() < 6){
                        codeList.add(editable.toString());
                        showCode();
                    }
                }
            }
        });

        // 监听验证码删除按键
        et_code.setOnKeyListener((view, keyCode, keyEvent) -> {
            if (keyCode == KeyEvent.KEYCODE_DEL && keyEvent.getAction() == KeyEvent.ACTION_DOWN && codeList.size() > 0) {
                codeList.remove(codeList.size()-1);
                showCode();
                return true;
            }
            return false;
        });
    }

    private void initView() {
        methodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        LayoutInflater.from(mContext).inflate(R.layout.x3_login_code_view, this);

        tv_code1 = findViewById(R.id.tv_code1);
        tv_code2 = findViewById(R.id.tv_code2);
        tv_code3 = findViewById(R.id.tv_code3);
        tv_code4 = findViewById(R.id.tv_code4);
        tv_code5 = findViewById(R.id.tv_code5);
        tv_code6 = findViewById(R.id.tv_code6);
        et_code = findViewById(R.id.et_code);
        v1 = findViewById(R.id.v1);
        v2 = findViewById(R.id.v2);
        v3 = findViewById(R.id.v3);
        v4 = findViewById(R.id.v4);
        v5 = findViewById(R.id.v5);
        v6 = findViewById(R.id.v6);
    }

    /**
     * 显示输入的验证码
     */
    private void showCode(){
        String code1 = "";
        String code2 = "";
        String code3 = "";
        String code4 = "";
        String code5 = "";
        String code6 = "";
        if(codeList.size() >= 1){
            code1 = codeList.get(0);
        }
        if(codeList.size() >= 2){
            code2 = codeList.get(1);
        }
        if(codeList.size() >= 3){
            code3 = codeList.get(2);
        }
        if(codeList.size() >= 4){
            code4 = codeList.get(3);
        }
        if(codeList.size() >= 5){
            code5 = codeList.get(4);
        }
        if(codeList.size() >= 6){
            code6 = codeList.get(5);
        }

        tv_code1.setText(code1);
        tv_code2.setText(code2);
        tv_code3.setText(code3);
        tv_code4.setText(code4);
        tv_code5.setText(code5);
        tv_code6.setText(code6);

        setColor();//设置高亮颜色
        callBack();//回调
    }

    /**
     * 设置高亮颜色
     */
    private void setColor(){
        int color_default = Color.parseColor("#999999");
        int color_focus = Color.parseColor("#0077ff");
        v1.setBackgroundColor(color_default);
        v2.setBackgroundColor(color_default);
        v3.setBackgroundColor(color_default);
        v4.setBackgroundColor(color_default);
        v5.setBackgroundColor(color_default);
        v6.setBackgroundColor(color_default);

        if(codeList.size() == 0){
            v1.setBackgroundColor(color_focus);
        }
        if(codeList.size() == 1){
            v2.setBackgroundColor(color_focus);
        }
        if(codeList.size() == 2){
            v3.setBackgroundColor(color_focus);
        }
        if(codeList.size() == 3){
            v4.setBackgroundColor(color_focus);
        }
        if(codeList.size() == 4){
            v5.setBackgroundColor(color_focus);
        }
        if(codeList.size() >= 5){
            v6.setBackgroundColor(color_focus);
        }
    }

    /**
     * 回调
     */
    private void callBack(){
        if(onInputListener==null){
            return;
        }
        if(codeList.size() == 6){
            onInputListener.onSucess(getPhoneCode());
        }else{
            onInputListener.onInput();
        }
    }

    //定义回调
    public interface OnInputListener{
        /**
         * 输入成功
         * @param code
         */
        void onSucess(String code);

        /**
         * 输入状态
         */
        void onInput();
    }
    private OnInputListener onInputListener;

    public void setOnInputListener(OnInputListener onInputListener){
        this.onInputListener = onInputListener;
    }

    /**
     * 显示键盘
     */
    public void showSoftInput(){
        //显示软键盘
        if(methodManager!=null && et_code!=null) {
            et_code.postDelayed(() -> methodManager.showSoftInput(et_code, 0),200);
        }
    }

    public void reset() {
        codeList.clear();
        showCode();
    }

    /**
     * 获得手机号验证码
     * @return 验证码
     */
    public String getPhoneCode(){
        StringBuilder sb = new StringBuilder();
        for (String code : codeList) {
            sb.append(code);
        }
        return sb.toString();
    }
}
